BEGIN {
  phase = "DATE"
}
/;;/ {
  next
}
$1 ~ "-" || $1 ~ "/", $1 == "\n" {
  cor = $1
  if(cor ~ "/") {
    gsub("/", "-", cor);
    phase = "DATE"
    isDate = 0
  }
  if(cor ~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/) {
    if(total > 0.009 || total < -0.009) {
      print "aledger error, doesn't evaluate to zero. Line" NR, $0
      exit 1
    }
    isDate = 1;
    phase = "DATE"
    total = 0
    skip = 0;
  }
  if(!skip) {
    if(phase == "DATE") {
      if(isDate) {
        if(start != "" && $1 < start)
          skip = 1;
        if(end != "" && $1 > end)
          exit 0
        isDate = 0;
        entrytotal = 0;
        phase = "ENTRIES";
      } else {
        print "aledger error, failed to parse date at on line " NR ": " $0
        error = 1
        exit 1
      }
    } else if(phase == "ENTRIES") {
      isDate = 0;
      if($1 ~ ":") {
        sub("\\$", "", $2)
        entry[$1] += $2
        total += $2
      }
    }
  }
}

END {
  if(total > 0.009 || total < -0.009) {
   print "aledger error, doesn't evaluate to zero. Line " NR "," $0
   exit 1
  }
  if(!error) {
    for(i in entry) {
     printf("%s %.2f\n", i, entry[i])
    }
  }
}
